package org.jeecg.modules.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.service.*;
import org.jeecg.modules.business.util.MapUtil;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.impl.QuartzJobServiceImpl;
import org.jeecg.modules.rooms.DTO.CesAllDayPriceRuleDto;
import org.jeecg.modules.rooms.entity.CesHousePriceScheme;
import org.jeecg.modules.rooms.service.CesAllDayPriceRuleServiceImpl;
import org.jeecg.modules.rooms.service.CesHousePriceSchemeServiceImpl;
import org.jeecg.modules.system.entity.SysTenant;
import org.jeecg.modules.system.service.ISysTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

 /**
 * @Description: bus_hotel
 * @Author: jeecg-boot
 * @Date:   2023-02-14
 * @Version: V1.0
 */
@Api(tags="bus_hotel")
@RestController
@RequestMapping("/business/busHotel")
@Slf4j
public class BusHotelController extends JeecgController<BusHotel, IBusHotelService> {
	@Autowired
	private IBusHotelService busHotelService;

	 @Autowired
	 private ISysTenantService sysTenantService;

	 @Autowired
	 private IBusDictService busDictService;
	 @Autowired
	 private IBusPrintTemplateService busPrintTemplateService;
	 @Autowired
	 private IBusNightTrialService busNightTrialService;
	 @Autowired
	 private IBusClearService busClearService;
	 @Autowired
	 private IBusHouseSettingService busHouseSettingService;
	 @Autowired
	 private IBusDepositSettingService busDepositSettingService;
	 @Autowired
	 private IBusCancelService busCancelService;
	 @Autowired
	 private IBusOtherRoomCardService busOtherRoomCardService;
	 @Autowired
	 private IBusOtherMemberVerifyService busOtherMemberVerifyService;
	 @Autowired
	 private IBusOtherEmptyDirtyRoomService busOtherEmptyDirtyRoomService;
	 @Autowired
	 private IBusOtherCheckoutService busOtherCheckoutService;
	 @Autowired
	 private IBusParamWeepService busParamWeepService;
	 @Autowired
	 private IBusParamShareService busParamShareService;
	 @Autowired
	 private IBusParamPresetService busParamPresetService;
	 @Autowired
	 private IBusParamComputeFormulaService busParamComputeFormulaService;
	 @Autowired
	 private IBusParamPrintHideService busParamPrintHideService;
	 @Autowired
	 private IBusParamPresetSyncService busParamPresetSyncService;
	 @Autowired
	 private IBusParamXcxRoomService busParamXcxRoomService;
	 @Resource
	 CesAllDayPriceRuleServiceImpl cesAllDayPriceRuleService;
	 @Resource
	 CesHousePriceSchemeServiceImpl housePriceSchemeService;

	 @Resource
	 IBusRoomPayTypeService roomPayTypeService;

	 @Resource
	 IQuartzJobService quartzJobService;

	/**
	 * 分页列表查询
	 *
	 * @param busHotel
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "bus_hotel-分页列表查询")
	@ApiOperation(value="bus_hotel-分页列表查询", notes="bus_hotel-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BusHotel>> queryPageList(BusHotel busHotel,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusHotel> queryWrapper = QueryGenerator.initQueryWrapper(busHotel, req.getParameterMap());
		LoginUser user = TokenUtils.getAuthUser();
		if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
			queryWrapper.eq("tenant_id",user.getRelTenantIds());
		}
		if (StringUtils.isNotEmpty(user.getDepartIds())){
			queryWrapper.in("id",Arrays.asList(user.getDepartIds().split(",")));
		}
		if (StringUtils.isNotEmpty(busHotel.getTenantId())){
			queryWrapper.eq("tenant_id",busHotel.getTenantId());
		}

		if (StringUtils.isNotEmpty(busHotel.getKeyWord())){
			queryWrapper.like("name",busHotel.getKeyWord());
		}




		Page<BusHotel> page = new Page<BusHotel>(pageNo, pageSize);
		IPage<BusHotel> pageList = busHotelService.page(page, queryWrapper);
//		pageList.getRecords().forEach(item->{
//			item.tenantName(useDepNames.get(item.getId()));
//		});

		pageList.getRecords().forEach(item->{
			SysTenant sysTenant = sysTenantService.getById(item.getTenantId());
			if (sysTenant != null) {
				item.setTenantName(sysTenant.getName());
			}
		});
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param busHotel
	 * @return
	 */
	@AutoLog(value = "bus_hotel-添加")
	@ApiOperation(value="bus_hotel-添加", notes="bus_hotel-添加")
	//@RequiresPermissions("business:bus_hotel_info:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BusHotel busHotel) {
		busHotel.setStatus(1);
		busHotel.setDelFlag(CommonConstant.DEL_FLAG_0);
		busHotel.setCheckStatus(0);
		LoginUser user = TokenUtils.getAuthUser();
		if(busHotel.getTenantId() == null || busHotel.getTenantId().equals("")){
			if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				busHotel.setTenantId(user.getRelTenantIds());
			} else {
				throw new JeecgBootException("当前登录人租户信息错误");
			}
		}
		busHotelService.save(busHotel);

		//添加酒店后需要初始化部分数据
		//初始化系统设置-字典管理
		LambdaQueryWrapper<BusDict> queryDict = new LambdaQueryWrapper<BusDict>();
		queryDict.eq(BusDict::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryDict.eq(BusDict::getHotelId, "0");
		queryDict.eq(BusDict::getTenantId, "0");
		List<BusDict> listDict = busDictService.list(queryDict);
		if (listDict != null && listDict.size() > 0){
			List<BusDict> batchModels = new ArrayList<>();
			listDict.forEach( item -> {
				BusDict newModel = new BusDict();
				newModel.setParentId("0");
				newModel.setTenantId(busHotel.getTenantId());
				newModel.setHotelId(busHotel.getId());
				newModel.setDictName(item.getDictName());
				newModel.setDictCode(item.getDictCode());
				newModel.setDescription(item.getDescription());
				newModel.setDelFlag(item.getDelFlag());
				batchModels.add(newModel);
//				busDictService.save(newDict);
			});
			busDictService.saveBatch(batchModels);
		}

		//添加酒店后初始化收费方式
		LambdaQueryWrapper<BusRoomPayType> queryPayType = new LambdaQueryWrapper<>();
		queryPayType.eq(BusRoomPayType::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryPayType.eq(BusRoomPayType::getHotelId, "0");
		queryPayType.eq(BusRoomPayType::getTenantId, "0");
		queryPayType.eq(BusRoomPayType::getIsMust, true);
		List<BusRoomPayType> payTypeList = roomPayTypeService.list(queryPayType);
		if (payTypeList != null && !payTypeList.isEmpty()){
			List<BusRoomPayType> batchModels = new ArrayList<>();
			payTypeList.forEach( item -> {
				BusRoomPayType newModel = new BusRoomPayType();
				newModel.setTenantId(busHotel.getTenantId());
				newModel.setHotelId(busHotel.getId());
				newModel.setName(item.getName());
				newModel.setIsDeposit(item.getIsDeposit());
				newModel.setIsClosing(item.getIsClosing());
				newModel.setIsMust(item.getIsMust());
				newModel.setStatus(item.getStatus());
				newModel.setDelFlag(item.getDelFlag());
				batchModels.add(newModel);
			});
			roomPayTypeService.saveBatch(batchModels);
		}

		//初始化打印模板
		LambdaQueryWrapper<BusPrintTemplate> queryPrintTemplate = new LambdaQueryWrapper<BusPrintTemplate>();
		queryPrintTemplate.eq(BusPrintTemplate::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryPrintTemplate.eq(BusPrintTemplate::getHotelId, "0");
		queryPrintTemplate.eq(BusPrintTemplate::getTenantId, "0");
		List<BusPrintTemplate> listPrintTemplate = busPrintTemplateService.list(queryPrintTemplate);
		if (listPrintTemplate != null && listPrintTemplate.size() > 0){
			List<BusPrintTemplate> batchModels = new ArrayList<>();
			listPrintTemplate.forEach( item -> {
				BusPrintTemplate newModel = new BusPrintTemplate();
				newModel.setTenantId(busHotel.getTenantId());
				newModel.setHotelId(busHotel.getId());
				newModel.setName(item.getName());
				newModel.setType(item.getType());
				newModel.setSpecs(item.getSpecs());
				newModel.setRemarks(item.getRemarks());
				newModel.setDefaultPrint(item.getDefaultPrint());
				newModel.setPrintType(item.getPrintType());
				newModel.setPrintSum(item.getPrintSum());
				newModel.setPreview(item.getPreview());
				newModel.setPrintContentOffset(item.getPrintContentOffset());
				newModel.setPrintPaperOffset(item.getPrintPaperOffset());
				newModel.setIsShowRefund(item.getIsShowRefund());
				newModel.setIsShowIdcard(item.getIsShowIdcard());
				newModel.setIsShowBreakfast(item.getIsShowBreakfast());
				newModel.setIsShowAddress(item.getIsShowAddress());
				newModel.setIsShowPhone(item.getIsShowPhone());
				newModel.setDelFlag(item.getDelFlag());
				batchModels.add(newModel);
			});
			busPrintTemplateService.saveBatch(batchModels);
		}
		//初始化参数设置
		//夜审设置
		LambdaQueryWrapper<BusNightTrial> queryNightTrial = new LambdaQueryWrapper<BusNightTrial>();
		queryNightTrial.eq(BusNightTrial::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryNightTrial.eq(BusNightTrial::getHotelId, "0");
		queryNightTrial.eq(BusNightTrial::getTenantId, "0");
		BusNightTrial busNightTrial = busNightTrialService.getOne(queryNightTrial);
		if (busNightTrial != null){
			BusNightTrial newModel = new BusNightTrial();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setAutoCheck(busNightTrial.getAutoCheck());
			newModel.setAutoHand(busNightTrial.getAutoHand());
			newModel.setLastNightState(busNightTrial.getLastNightState());
			newModel.setLastNightTrialTime(busNightTrial.getLastNightTrialTime());
			newModel.setSetNightTrialTime(busNightTrial.getSetNightTrialTime());
			newModel.setDelFlag(busNightTrial.getDelFlag());
			busNightTrialService.save(newModel);
		}

		//清扫房间选择服务员
		LambdaQueryWrapper<BusClear> queryClear = new LambdaQueryWrapper<BusClear>();
		queryClear.eq(BusClear::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryClear.eq(BusClear::getHotelId, "0");
		queryClear.eq(BusClear::getTenantId, "0");
		BusClear busClear = busClearService.getOne(queryClear);
		if (busClear != null){
			BusClear newModel = new BusClear();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busClear.getStatus());
			newModel.setDelFlag(busClear.getDelFlag());
			busClearService.save(newModel);
		}

		//房务管理
		LambdaQueryWrapper<BusHouseSetting> queryHouseSetting= new LambdaQueryWrapper<BusHouseSetting>();
		queryHouseSetting.eq(BusHouseSetting::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryHouseSetting.eq(BusHouseSetting::getHotelId, "0");
		queryHouseSetting.eq(BusHouseSetting::getTenantId, "0");
		BusHouseSetting busHouseSetting = busHouseSettingService.getOne(queryHouseSetting);
		if (busHouseSetting != null){
			BusHouseSetting newModel = new BusHouseSetting();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setDirtyHouse(busHouseSetting.getDirtyHouse());
			newModel.setDirtyRoom(busHouseSetting.getDirtyRoom());
			newModel.setTDirtyHouse(busHouseSetting.getTDirtyHouse());
			newModel.setXDirtyHouse(busHouseSetting.getXDirtyHouse());
			newModel.setZDirtyHouse(busHouseSetting.getZDirtyHouse());
			newModel.setStatus(busHouseSetting.getStatus());
			newModel.setDelFlag(busHouseSetting.getDelFlag());
			busHouseSettingService.save(newModel);
		}

		//押金设置
		LambdaQueryWrapper<BusDepositSetting> queryDepositSetting = new LambdaQueryWrapper<BusDepositSetting>();
		queryDepositSetting.eq(BusDepositSetting::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryDepositSetting.eq(BusDepositSetting::getHotelId, "0");
		queryDepositSetting.eq(BusDepositSetting::getTenantId, "0");
		BusDepositSetting busDepositSetting = busDepositSettingService.getOne(queryDepositSetting);
		if (busDepositSetting != null){
			BusDepositSetting newModel = new BusDepositSetting();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setDepositType(busDepositSetting.getDepositType());
			newModel.setDepositMoney(busDepositSetting.getDepositMoney());
			newModel.setDepositMultiple(busDepositSetting.getDepositMultiple());
			newModel.setBalanceAlarm(busDepositSetting.getBalanceAlarm());
			newModel.setBalanceLess(busDepositSetting.getBalanceLess());
			newModel.setIsRounding(busDepositSetting.getIsRounding());
			newModel.setDelFlag(busDepositSetting.getDelFlag());
			busDepositSettingService.save(newModel);
		}
		//撤销入住
		LambdaQueryWrapper<BusCancel> queryCancel = new LambdaQueryWrapper<BusCancel>();
		queryCancel.eq(BusCancel::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryCancel.eq(BusCancel::getHotelId, "0");
		queryCancel.eq(BusCancel::getTenantId, "0");
		BusCancel busCancel = busCancelService.getOne(queryCancel);
		if (busCancel != null){
			BusCancel newModel = new BusCancel();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busCancel.getStatus());
			newModel.setMinute(busCancel.getMinute());
			newModel.setDelFlag(busCancel.getDelFlag());
			busCancelService.save(newModel);
		}

		//其他
		LambdaQueryWrapper<BusOtherRoomCard> queryOtherRoomCard = new LambdaQueryWrapper<BusOtherRoomCard>();
		queryOtherRoomCard.eq(BusOtherRoomCard::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryOtherRoomCard.eq(BusOtherRoomCard::getHotelId, "0");
		queryOtherRoomCard.eq(BusOtherRoomCard::getTenantId, "0");
		BusOtherRoomCard busOtherRoomCard = busOtherRoomCardService.getOne(queryOtherRoomCard);
		if (busOtherRoomCard != null){
			BusOtherRoomCard newModel = new BusOtherRoomCard();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busOtherRoomCard.getStatus());
			newModel.setLogoutCard(busOtherRoomCard.getLogoutCard());
			newModel.setTaxRate(busOtherRoomCard.getTaxRate());
			newModel.setDelFlag(busOtherRoomCard.getDelFlag());
			busOtherRoomCardService.save(newModel);
		}

		LambdaQueryWrapper<BusOtherMemberVerify> queryOtherMemberVerify = new LambdaQueryWrapper<BusOtherMemberVerify>();
		queryOtherMemberVerify.eq(BusOtherMemberVerify::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryOtherMemberVerify.eq(BusOtherMemberVerify::getHotelId, "0");
		queryOtherMemberVerify.eq(BusOtherMemberVerify::getTenantId, "0");
		BusOtherMemberVerify busOtherMemberVerify = busOtherMemberVerifyService.getOne(queryOtherMemberVerify);
		if (busOtherMemberVerify != null){
			BusOtherMemberVerify newModel = new BusOtherMemberVerify();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busOtherMemberVerify.getStatus());
			newModel.setDelFlag(busOtherMemberVerify.getDelFlag());
			busOtherMemberVerifyService.save(newModel);
		}

		LambdaQueryWrapper<BusOtherEmptyDirtyRoom> queryOtherEmptyDirtyRoom = new LambdaQueryWrapper<BusOtherEmptyDirtyRoom>();
		queryOtherEmptyDirtyRoom.eq(BusOtherEmptyDirtyRoom::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryOtherEmptyDirtyRoom.eq(BusOtherEmptyDirtyRoom::getHotelId, "0");
		queryOtherEmptyDirtyRoom.eq(BusOtherEmptyDirtyRoom::getTenantId, "0");
		BusOtherEmptyDirtyRoom busOtherEmptyDirtyRoom = busOtherEmptyDirtyRoomService.getOne(queryOtherEmptyDirtyRoom);
		if (busOtherEmptyDirtyRoom != null){
			BusOtherEmptyDirtyRoom newModel = new BusOtherEmptyDirtyRoom();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busOtherEmptyDirtyRoom.getStatus());
			newModel.setDelFlag(busOtherEmptyDirtyRoom.getDelFlag());
			busOtherEmptyDirtyRoomService.save(newModel);
		}

		LambdaQueryWrapper<BusOtherCheckout> queryOtherCheckout = new LambdaQueryWrapper<BusOtherCheckout>();
		queryOtherCheckout.eq(BusOtherCheckout::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryOtherCheckout.eq(BusOtherCheckout::getHotelId, "0");
		queryOtherCheckout.eq(BusOtherCheckout::getTenantId, "0");
		BusOtherCheckout busOtherCheckout = busOtherCheckoutService.getOne(queryOtherCheckout);
		if (busOtherCheckout != null){
			BusOtherCheckout newModel = new BusOtherCheckout();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busOtherCheckout.getStatus());
			newModel.setDelFlag(busOtherCheckout.getDelFlag());
			busOtherCheckoutService.save(newModel);
		}

		//扫码支付
		LambdaQueryWrapper<BusParamWeep> queryParamWeep = new LambdaQueryWrapper<BusParamWeep>();
		queryParamWeep.eq(BusParamWeep::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamWeep.eq(BusParamWeep::getHotelId, "0");
		queryParamWeep.eq(BusParamWeep::getTenantId, "0");
		BusParamWeep busParamWeep = busParamWeepService.getOne(queryParamWeep);
		if (busParamWeep != null){
			BusParamWeep newModel = new BusParamWeep();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setDayNum(busParamWeep.getDayNum());
			newModel.setDelFlag(busParamWeep.getDelFlag());
			busParamWeepService.save(newModel);
		}

		//数据共享
		LambdaQueryWrapper<BusParamShare> queryParamShare = new LambdaQueryWrapper<BusParamShare>();
		queryParamShare.eq(BusParamShare::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamShare.eq(BusParamShare::getHotelId, "0");
		queryParamShare.eq(BusParamShare::getTenantId, "0");
		BusParamShare busParamShare = busParamShareService.getOne(queryParamShare);
		if (busParamShare != null){
			BusParamShare newModel = new BusParamShare();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setAgreementAccountShare(busParamShare.getAgreementAccountShare());
			newModel.setAgreementShare(busParamShare.getAgreementShare());
			newModel.setMemberAccountShare(busParamShare.getMemberAccountShare());
			newModel.setMemberShare(busParamShare.getMemberShare());
			newModel.setDelFlag(busParamShare.getDelFlag());
			busParamShareService.save(newModel);
		}

		//预定参数
		LambdaQueryWrapper<BusParamPreset> queryParamPreset = new LambdaQueryWrapper<BusParamPreset>();
		queryParamPreset.eq(BusParamPreset::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamPreset.eq(BusParamPreset::getHotelId, "0");
		queryParamPreset.eq(BusParamPreset::getTenantId, "0");
		BusParamPreset busParamPreset = busParamPresetService.getOne(queryParamPreset);
		if (busParamPreset != null){
			BusParamPreset newModel = new BusParamPreset();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busParamPreset.getStatus());
			newModel.setMinute(busParamPreset.getMinute());
			newModel.setDelFlag(busParamPreset.getDelFlag());
			busParamPresetService.save(newModel);
		}

		//计算公式
		LambdaQueryWrapper<BusParamComputeFormula> queryParamComputeFormula = new LambdaQueryWrapper<BusParamComputeFormula>();
		queryParamComputeFormula.eq(BusParamComputeFormula::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamComputeFormula.eq(BusParamComputeFormula::getHotelId, "0");
		queryParamComputeFormula.eq(BusParamComputeFormula::getTenantId, "0");
		BusParamComputeFormula busParamComputeFormula = busParamComputeFormulaService.getOne(queryParamComputeFormula);
		if (busParamComputeFormula != null){
			BusParamComputeFormula newModel = new BusParamComputeFormula();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setCardinal(busParamComputeFormula.getCardinal());
			newModel.setNights(busParamComputeFormula.getNights());
			newModel.setStatus(busParamComputeFormula.getStatus());
			newModel.setDelFlag(busParamComputeFormula.getDelFlag());
			busParamComputeFormulaService.save(newModel);
		}

		//打印单据隐藏信息设置
		LambdaQueryWrapper<BusParamPrintHide> queryParamPrintHide = new LambdaQueryWrapper<BusParamPrintHide>();
		queryParamPrintHide.eq(BusParamPrintHide::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamPrintHide.eq(BusParamPrintHide::getHotelId, "0");
		queryParamPrintHide.eq(BusParamPrintHide::getTenantId, "0");
		BusParamPrintHide busParamPrintHide = busParamPrintHideService.getOne(queryParamPrintHide);
		if (busParamPrintHide != null){
			BusParamPrintHide newModel = new BusParamPrintHide();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setGuest(busParamPrintHide.getGuest());
			newModel.setPrice(busParamPrintHide.getPrice());
			newModel.setDelFlag(busParamPrintHide.getDelFlag());
			busParamPrintHideService.save(newModel);
		}

		//线上预定房量是否同步
		LambdaQueryWrapper<BusParamPresetSync> queryParamPresetSync = new LambdaQueryWrapper<BusParamPresetSync>();
		queryParamPresetSync.eq(BusParamPresetSync::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamPresetSync.eq(BusParamPresetSync::getHotelId, "0");
		queryParamPresetSync.eq(BusParamPresetSync::getTenantId, "0");
		BusParamPresetSync busParamPresetSync = busParamPresetSyncService.getOne(queryParamPresetSync);
		if (busParamPresetSync != null){
			BusParamPresetSync newModel = new BusParamPresetSync();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setStatus(busParamPresetSync.getStatus());
			newModel.setDelFlag(busParamPresetSync.getDelFlag());
			busParamPresetSyncService.save(newModel);
		}
		//小程序客房服务设置
		LambdaQueryWrapper<BusParamXcxRoom> queryParamXcxRoom = new LambdaQueryWrapper<BusParamXcxRoom>();
		queryParamXcxRoom.eq(BusParamXcxRoom::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
		queryParamXcxRoom.eq(BusParamXcxRoom::getHotelId, "0");
		queryParamXcxRoom.eq(BusParamXcxRoom::getTenantId, "0");
		BusParamXcxRoom busParamXcxRoom = busParamXcxRoomService.getOne(queryParamXcxRoom);
		if (busParamXcxRoom != null){
			BusParamXcxRoom newModel = new BusParamXcxRoom();
			newModel.setTenantId(busHotel.getTenantId());
			newModel.setHotelId(busHotel.getId());
			newModel.setCheckOut(busParamXcxRoom.getCheckOut());
			newModel.setCheckOutType(busParamXcxRoom.getCheckOutType());
			newModel.setKeepLive(busParamXcxRoom.getKeepLive());
			newModel.setMessageLive(busParamXcxRoom.getMessageLive());
			newModel.setServiceLive(busParamXcxRoom.getServiceLive());
			newModel.setRepairLive(busParamXcxRoom.getRepairLive());
			newModel.setDelFlag(busParamXcxRoom.getDelFlag());
			busParamXcxRoomService.save(newModel);
		}

		//定时任务
		ArrayList<QuartzJob> quartzJobs = new ArrayList<>();
		QuartzJob quartzJob1 = new QuartzJob();
		quartzJob1.setDelFlag(CommonConstant.DEL_FLAG_0);
		quartzJob1.setJobClassName("org.jeecg.modules.quartz.job.LivingRoomOrderFeeJob");
		quartzJob1.setCronExpression("0 */1 * * * ?");
		quartzJob1.setParameter(busHotel.getId());
		quartzJob1.setStatus(CommonConstant.STATUS_DISABLE);

		QuartzJob quartzJob2 = new QuartzJob();
		quartzJob2.setDelFlag(CommonConstant.DEL_FLAG_0);
		quartzJob2.setJobClassName("org.jeecg.modules.quartz.job.NightAuditJob");
		quartzJob2.setCronExpression("0 00 06 * * ? ");
		quartzJob2.setParameter(busHotel.getId());
		quartzJob2.setStatus(CommonConstant.STATUS_DISABLE);
		quartzJobs.add(quartzJob1);
		quartzJobs.add(quartzJob2);
		quartzJobService.saveBatch(quartzJobs);
		quartzJobService.resumeJob(quartzJob1);
		quartzJobService.resumeJob(quartzJob2);

		//全天房计费规则
		CesAllDayPriceRuleDto cesAllDayPriceRuleDto = new CesAllDayPriceRuleDto();
		cesAllDayPriceRuleDto.setHotelId(busHotel.getId());
		cesAllDayPriceRuleDto.setEnterTime("6:00");
		cesAllDayPriceRuleDto.setLeaveTime("14:00");
		cesAllDayPriceRuleDto.setTimeOutRule(1);
		cesAllDayPriceRuleDto.setRuleType(1);
		cesAllDayPriceRuleDto.setMinute(30);
		cesAllDayPriceRuleDto.setPrice(BigDecimal.valueOf(10));
		cesAllDayPriceRuleDto.setMoreThenMinute(10);
		cesAllDayPriceRuleDto.setMoreThenPrice(BigDecimal.valueOf(5));
		cesAllDayPriceRuleDto.setEndTime("18:00");
		cesAllDayPriceRuleDto.setDayTime("30");
		cesAllDayPriceRuleDto.setInvalid(false);
		cesAllDayPriceRuleService.modifyWholeDayUnifyPrice(cesAllDayPriceRuleDto);
		// 基础房价方案 平日方案，周末方案，节假日方案
		List<CesHousePriceScheme> priceSchemeList = new ArrayList<>(3);
		CesHousePriceScheme priceScheme = new CesHousePriceScheme();
		CesHousePriceScheme weekScheme = new CesHousePriceScheme();
		CesHousePriceScheme holidayScheme = new CesHousePriceScheme();
		priceSchemeList.add(priceScheme);
		priceSchemeList.add(weekScheme);
		priceSchemeList.add(holidayScheme);
		priceScheme.setSchemeType(0);
		priceScheme.setCheckInTime(0);
		priceScheme.setValidTime(0);
		priceScheme.setOpen(1);
		priceScheme.setHotelId(busHotel.getId());
		BeanUtil.copyProperties(priceScheme, weekScheme);
		weekScheme.setSchemeType(1);
		BeanUtil.copyProperties(priceScheme, holidayScheme);
		holidayScheme.setSchemeType(2);
		housePriceSchemeService.saveBatch(priceSchemeList);

		return Result.OK("添加成功！");

	}


	/**
	 *  编辑
	 *
	 * @param busHotel
	 * @return
	 */
	@AutoLog(value = "bus_hotel-编辑")
	@ApiOperation(value="bus_hotel-编辑", notes="bus_hotel-编辑")
	//@RequiresPermissions("business:bus_hotel_info:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BusHotel busHotel) {
		if(busHotel.getTenantId() == null || busHotel.getTenantId().equals("")){
			LoginUser user = TokenUtils.getAuthUser();
			if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				busHotel.setTenantId(user.getRelTenantIds());
			} else {
				throw new JeecgBootException("当前登录人租户信息错误");
			}
		}
		busHotelService.updateById(busHotel);
		return Result.OK("编辑成功!");
	}

	 /**
	  *  编辑门店基本信息
	  *
	  * @param busHotel
	  * @return
	  */
	 @AutoLog(value = "bus_hotel-编辑门店基本信息")
	 @ApiOperation(value="bus_hotel-编辑门店基本信息", notes="bus_hotel-编辑门店基本信息")
	 //@RequiresPermissions("business:bus_hotel_info:edit")
	 @RequestMapping(value = "/editMainInfo", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<BusHotel> editMainInfo(@RequestBody BusHotel busHotel, HttpServletRequest request) {
		 if(busHotel.getTenantId() == null || busHotel.getTenantId().equals("")){
			 LoginUser user = TokenUtils.getAuthUser();
			 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				 busHotel.setTenantId(user.getRelTenantIds());
			 } else {
				 throw new JeecgBootException("当前登录人租户信息错误");
			 }
		 }
		 BusHotel editHotel = busHotelService.getById(busHotel.getId());//new BusHotel();
//		 editHotel.setId(busHotel.getId());
		 editHotel.setName(busHotel.getName());
		 editHotel.setStar(busHotel.getStar());

		 editHotel.setAddress(busHotel.getAddress());
		 editHotel.setLng(busHotel.getLng());
		 editHotel.setLat(busHotel.getLat());
		 editHotel.setLinkName(busHotel.getLinkName());
		 editHotel.setLinkTel(busHotel.getLinkTel());
		 editHotel.setTel(busHotel.getTel());
		 editHotel.setOpenTime(busHotel.getOpenTime());
		 editHotel.setRenovationTime(busHotel.getRenovationTime());
		 //集团品牌
		 editHotel.setRule(busHotel.getRule());
		 editHotel.setTagList(busHotel.getTagList());
		 editHotel.setFacility(busHotel.getFacility());
		 editHotel.setTopic(busHotel.getTopic());
		 editHotel.setUnionpay(busHotel.getUnionpay());
		 editHotel.setWxOpen(busHotel.getWxOpen());
		 editHotel.setDdOpen(busHotel.getDdOpen());
		 editHotel.setYeOpen(busHotel.getYeOpen());
		 editHotel.setPolicy(busHotel.getPolicy());
		 editHotel.setIntroduction(busHotel.getIntroduction());
		 editHotel.setReserveRemind(busHotel.getReserveRemind());
		 editHotel.setTraffic(busHotel.getTraffic());

		 editHotel.setUpdateBy(JwtUtil.getUserNameByToken(request));
		 editHotel.setUpdateTime(new Date());
		 busHotelService.editHotelInfo(editHotel);
//		 BusHotel result = busHotelService.getById(busHotel.getId());
		 return Result.OK(editHotel);
	 }

	 /**
	  *  编辑门店图片信息
	  *
	  * @param busHotel
	  * @return
	  */
	 @AutoLog(value = "bus_hotel-编辑门店图片信息")
	 @ApiOperation(value="bus_hotel-编辑门店图片信息", notes="bus_hotel-编辑门店图片信息")
	 //@RequiresPermissions("business:bus_hotel_info:edit")
	 @RequestMapping(value = "/editImage", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<BusHotel> editImage(@RequestBody BusHotel busHotel, HttpServletRequest request) {
		 if(busHotel.getTenantId() == null || busHotel.getTenantId().equals("")){
			 LoginUser user = TokenUtils.getAuthUser();
			 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				 busHotel.setTenantId(user.getRelTenantIds());
			 } else {
				 throw new JeecgBootException("当前登录人租户信息错误");
			 }
		 }
		 BusHotel editHotel = busHotelService.getById(busHotel.getId());//new BusHotel();
//		 editHotel.setId(busHotel.getId());
		 editHotel.setEwmLogo(busHotel.getEwmLogo());
		 editHotel.setImgs(busHotel.getImgs());

		 editHotel.setUpdateBy(JwtUtil.getUserNameByToken(request));
		 editHotel.setUpdateTime(new Date());
		 busHotelService.editHotelInfo(editHotel);
//		 BusHotel result = busHotelService.getById(busHotel.getId());
		 return Result.OK(editHotel);
	 }

	 /**
	  *  编辑门店认证信息
	  *
	  * @param busHotel
	  * @return
	  */
	 @AutoLog(value = "bus_hotel-编辑门店认证信息")
	 @ApiOperation(value="bus_hotel-编辑门店认证信息", notes="bus_hotel-编辑门店认证信息")
	 //@RequiresPermissions("business:bus_hotel_info:edit")
	 @RequestMapping(value = "/editAuthInfo", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<BusHotel> editAuthInfo(@RequestBody BusHotel busHotel, HttpServletRequest request) {
		 if(busHotel.getTenantId() == null || busHotel.getTenantId().equals("")){
			 LoginUser user = TokenUtils.getAuthUser();
			 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
				 busHotel.setTenantId(user.getRelTenantIds());
			 } else {
				 throw new JeecgBootException("当前登录人租户信息错误");
			 }
		 }
		 BusHotel editHotel = busHotelService.getById(busHotel.getId());//new BusHotel();
//		 BusHotel oldHotel = busHotelService.getById(busHotel.getId());
		 //认证信息必须与酒店联系人一致
		 if (editHotel == null ){
			 throw new JeecgBootException("当前编辑信息不存在");
		 }
		 if(!editHotel.getLinkName().equals(busHotel.getSfzName())){
			 throw new JeecgBootException("当前实名信息与酒店联系人信息不一致");
		 }
		 editHotel.setId(busHotel.getId());
		 editHotel.setYyImg(busHotel.getYyImg());
		 editHotel.setTzhyImg(busHotel.getTzhyImg());
		 editHotel.setLegalPerson(busHotel.getLegalPerson());
		 editHotel.setRegisteredAddress(busHotel.getRegisteredAddress());
		 editHotel.setBusinessTerm(busHotel.getBusinessTerm());
		 editHotel.setSfzImg1(busHotel.getSfzImg1());
		 editHotel.setSfzImg2(busHotel.getSfzImg2());
		 editHotel.setSfzImg3(busHotel.getSfzImg3());
		 editHotel.setSfzName(busHotel.getSfzName());
		 editHotel.setIdCard(busHotel.getIdCard());

		 editHotel.setEwmLogo(busHotel.getEwmLogo());
		 editHotel.setImgs(busHotel.getImgs());

		 editHotel.setUpdateBy(JwtUtil.getUserNameByToken(request));
		 editHotel.setUpdateTime(new Date());
		 busHotelService.editHotelInfo(editHotel);
//		 BusHotel result = busHotelService.getById(busHotel.getId());
		 return Result.OK(editHotel);
	 }

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "bus_hotel-通过id删除")
	@ApiOperation(value="bus_hotel-通过id删除", notes="bus_hotel-通过id删除")
	//@RequiresPermissions("business:bus_hotel_info:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		busHotelService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "bus_hotel-批量删除")
	@ApiOperation(value="bus_hotel-批量删除", notes="bus_hotel-批量删除")
	//@RequiresPermissions("business:bus_hotel_info:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busHotelService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "bus_hotel-通过id查询")
	@ApiOperation(value="bus_hotel-通过id查询", notes="bus_hotel-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BusHotel> queryById(@RequestParam(name="id",required=true) String id) {
		BusHotel busHotel = busHotelService.getById(id);
		if(busHotel==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(busHotel);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busHotel
    */
    //@RequiresPermissions("business:bus_hotel_info:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusHotel busHotel) {
        return super.exportXls(request, busHotel, BusHotel.class, "bus_hotel");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("business:bus_hotel_info:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BusHotel.class);
    }

	 /**
	  * 信息审核
	  */
	 @RequestMapping(value = "/hotelCheck", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<?> hotelCheck(@RequestBody BusHotel busHotel, HttpServletRequest request) {
		 BusHotel checkHotel = new BusHotel();
		 checkHotel.setId(busHotel.getId());
		 checkHotel.setCheckRemarks(busHotel.getCheckRemarks());
		 checkHotel.setCheckStatus(busHotel.getCheckStatus());
		 checkHotel.setCheckBy(JwtUtil.getUserNameByToken(request));
		 checkHotel.setCheckTime(new Date());
		 busHotelService.hotelCheck(checkHotel);
		 return Result.OK("审核成功!");
	 }

	 /**
	  * 通过地址获取经纬度信息
	  *
	  * @param address
	  * @return
	  */
	 @RequestMapping(value = "/getLonAndLat", method = RequestMethod.GET)
	 public Result<?> getLonAndLat(String address) {
		 Map<String, String> lonAndLat = MapUtil.getLonAndLat(address, "e5f8153683e606345c7c96cd3e16172f");
		 System.out.println("转换后经纬度为：" + lonAndLat);
		 return Result.OK(lonAndLat);
	 }

	 /**
	  * 列表查询
	  *
	  * @param busHotel
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "bus_hotel-列表查询")
	 @ApiOperation(value="bus_hotel-列表查询", notes="bus_hotel-列表查询")
	 @GetMapping(value = "/queryList")
	 public Result<List<BusHotel>> queryList(BusHotel busHotel, HttpServletRequest req) {
		 QueryWrapper<BusHotel> queryWrapper = QueryGenerator.initQueryWrapper(busHotel, req.getParameterMap());
		 LoginUser user = TokenUtils.getAuthUser();
		 if(user.getRelTenantIds() != null && !user.getRelTenantIds().equals("")){
			 queryWrapper.eq("tenant_id",user.getRelTenantIds());
		 }
		 queryWrapper.eq("status",1);
		 queryWrapper.eq("check_status",1);
		 queryWrapper.eq("del_flag",0);
		 List<BusHotel> list = busHotelService.list(queryWrapper);
		 return Result.OK(list);
	 }

	 /**
	  * 设置入住付款状态
	  * @return
	  */
	 @ApiOperation(value="设置入住付款状态", notes="设置入住付款状态")
	 @GetMapping(value = "/set-pay-state")
	 public Result<Boolean> setPayState(String hotelId,String type) {
		 BusHotel hotel = busHotelService.getById(hotelId);
		 if(hotel==null) throw new JeecgBootException("酒店不存在");
		 hotel.setRemenmberPayState(type);
		 busHotelService.updateById(hotel);
		 return Result.OK(true);
	 }
 }
